/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:FileUtil.java
 * Version:1.0
 * Date:2011-5-27
 * Description: 操作文件的工具类
 * author:zhijian.zhang
 */
package com.tietiao.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

public class FileUtil {
	/**
	 * 日志对象
	 */
	private static Logger logger = Logger.getLogger(FileUtil.class);

	private static final int BUFFER_SIZE = 16 * 1024;

	/**
	 * 功能：读取给定文件内容
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-5-27
	 * 
	 * @param filePath
	 *            给定的文件路径
	 * @return
	 */
	public static String readFile(String filePath) {

		File file = new File(filePath);

		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			BufferedReader bff = new BufferedReader(new InputStreamReader(
					fileInputStream, "UTF-8"));
			if (bff.ready()) {
				StringBuilder sb = new StringBuilder();
				String strLine = "";
				while (strLine != null) {
					strLine = bff.readLine();
					if (StringUtil.checkStrIsNotNull(strLine)) {
						sb.append(strLine);
					}
				}
				// 关闭流
				bff.close();
				fileInputStream.close();
				return sb.toString();
			}

		} catch (FileNotFoundException e) {
			logger.error("给定的文件【" + filePath + "】不存在！");
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 
	 * 功能：写入文件
	 * 
	 * @author: Sophie
	 * @Date: 2011-7-20
	 * 
	 * @param src
	 * @param dst
	 */
	public static void writeFile(File src, File dst) throws Exception {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst),
					BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			while (in.read(buffer) > 0) {
				out.write(buffer);
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (null != in) {
				in.close();
				in = null;
			}
			if (null != out) {
				out.close();
				out = null;
			}
		}
	}
	
	/**
	 * 
	 * 功能：将原文件数据拷贝至原目标文件中
	 * @author: zhijian.zhang
	 * @Date: 2012-1-4
	 *
	 * @param src
	 * @param outputpath 文件存放的路径（目录）
	 * @param fileName 文件名
	 * @throws Exception
	 */
	public static void writeFile(File src,String outputpath,String fileName) throws Exception{
		File outputFile = new File(outputpath);
		if(!outputFile.exists()) {
			outputFile.mkdirs();
		}
		File outfilepath = new File(outputpath +"\\" + fileName);
		writeFile(src,outfilepath);
	}

	/**
	 * 
	 * 功能：删除文件
	 * 
	 * @author: Sophie
	 * @Date: 2011-7-26
	 * 
	 * @param name
	 */
	public static void deleteFile(String name) {
		String path = ServletActionContext.getServletContext()
				.getRealPath(name);
		File pathFile = new File(path);
		if (pathFile.exists()) {
			pathFile.delete();
		}
	}

	public static void main(String[] args) throws Exception{
		File file = new File("E:\\pic_path\\1eb7900e-cedc-41bb-8cba-be2dad78ce56.jpg");
		writeFile(file, "E:/test/2012/11/12/13/14" ,"xx.jpg");
	}
}
